package institution_service

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/EDDYCJY/go-gin-example/models"
	"github.com/shopspring/decimal"
)

type GetGoods struct {
	InstitutionID int
	PageNum       int
	PageSize      int
}

func (t *GetGoods) GetGoods() (interface{}, error) {
	maps := map[string]interface{}{
		"institution_id": t.InstitutionID,
		"is_delete":      0,
	}
	total, _ := models.GetGoodsTotal(maps)
	goodsList, _ := models.GetGoodsPage([]string{"goods_id", "user_id", "supply_price", "streamer_id", "goods_name", "goods_spec", "goods_no", "selling_price", "goods_img", "create_time", "goods_num"}, maps, t.PageNum, t.PageSize, "create_time desc")
	var lists []interface{}
	for _, v := range goodsList {
		sellingPrice := decimal.NewFromInt(int64(v.SellingPrice)).Div(decimal.NewFromInt(100))
		supplyPrice := decimal.NewFromInt(int64(v.SupplyPrice)).Div(decimal.NewFromInt(100))
		var goods_tag []interface{}
		for _, v1 := range v.GoodsTag {
			var tag = map[string]interface{}{
				"tag_id":   v1.TagID,
				"tag_name": v1.Tag.TagName,
			}
			goods_tag = append(goods_tag, tag)
		}
		params := map[string]interface{}{
			"goods_id":        v.GoodsID,
			"goods_name":      v.GoodsName,
			"goods_spec":      v.GoodsSpec,
			"goods_no":        v.GoodsNo,
			"selling_price":   sellingPrice,
			"supply_price":    supplyPrice,
			"create_time":     v.CreateTime.Format("2006-01-02 15:04:05"),
			"goods_img":       v.GoodsImg,
			"goods_num":       v.GoodsNum,
			"streamer_name":   v.Streamer.StreamerName,
			"streamer_mobile": v.Streamer.StreamerMobile,
			"user_name":       v.User.UserName,
			"goods_tag":       goods_tag,
		}
		lists = append(lists, params)
	}
	return map[string]interface{}{
		"lists": lists,
		"total": total,
	}, nil
}

// Login 定义登录请求结构体
type AddGoods struct {
	InstitutionID int
	GoodsName     string
	GoodsSpec     string
	GoodsNo       string
	SupplyPrice   int
	SellingPrice  int
	GoodsImg      string
	GoodsNum      int
	UserID        int
	TagIds        string
}

// AddGoods 执行用户登录逻辑
func (t *AddGoods) AddGoods() (map[string]interface{}, error) {

	// 开启事务
	tx := models.GetDB().Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	// 更新商品信息
	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"goods_name":     t.GoodsName,
		"goods_spec":     t.GoodsSpec,
		"goods_no":       t.GoodsNo,
		"supply_price":   t.SupplyPrice,
		"selling_price":  t.SellingPrice,
		"goods_img":      t.GoodsImg,
		"goods_num":      t.GoodsNum,
		"user_id":        t.UserID,
	}
	goodsID, err := models.AddGoodsDB(maps, tx)
	if err != nil {
		return nil, err
	}

	// 清理商品标签
	maps = map[string]interface{}{
		"goods_id": goodsID,
	}
	err = models.CleanGoodsTagAllDB(maps, tx)
	if err != nil {
		tx.Rollback()
		return nil, err
	}

	// 添加新的商品标签
	var TagList []models.GoodsTagAll
	for _, v := range strings.Split(t.TagIds, ",") {
		TagId, _ := strconv.ParseInt(v, 0, 64)
		var TagInfo = models.GoodsTagAll{
			GoodsID: goodsID,
			TagId:   int(TagId),
		}
		TagList = append(TagList, TagInfo)
	}
	if len(TagList) > 0 {
		_, err = models.AddGoodsTagAllDB(TagList, tx)
		if err != nil {
			tx.Rollback()
			return nil, fmt.Errorf("添加商品标签失败: %v", err)
		}
	}

	// 提交事务
	err = tx.Commit().Error
	if err != nil {
		tx.Rollback()
		return nil, fmt.Errorf("提交事务失败: %v", err)
	}
	return nil, nil
}

// Login 定义登录请求结构体
type EditGoods struct {
	GoodsID       int
	InstitutionID int
	GoodsName     string
	GoodsSpec     string
	GoodsNo       string
	SupplyPrice   int
	SellingPrice  int
	GoodsImg      string
	GoodsNum      int
	TagIds        string
}

// EditGoods 执行用户登录逻辑
func (t *EditGoods) EditGoods() (map[string]interface{}, error) {
	// 开启事务
	tx := models.GetDB().Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	// 更新商品信息
	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"goods_id":       t.GoodsID,
	}
	var update = map[string]interface{}{
		"goods_name":    t.GoodsName,
		"goods_spec":    t.GoodsSpec,
		"goods_no":      t.GoodsNo,
		"supply_price":  t.SupplyPrice,
		"selling_price": t.SellingPrice,
		"goods_img":     t.GoodsImg,
		"goods_num":     t.GoodsNum,
	}
	err := models.EditGoodsDB(maps, update, tx)
	if err != nil {
		tx.Rollback()
		return nil, err
	}

	// 清理商品标签
	maps = map[string]interface{}{
		"goods_id": t.GoodsID,
	}
	err = models.CleanGoodsTagAllDB(maps, tx)
	if err != nil {
		tx.Rollback()
		return nil, err
	}

	// 添加新的商品标签
	var TagList []models.GoodsTagAll
	for _, v := range strings.Split(t.TagIds, ",") {
		TagId, _ := strconv.ParseInt(v, 0, 64)
		var TagInfo = models.GoodsTagAll{
			GoodsID: t.GoodsID,
			TagId:   int(TagId),
		}
		TagList = append(TagList, TagInfo)
	}
	if len(TagList) > 0 {
		_, err = models.AddGoodsTagAllDB(TagList, tx)
		if err != nil {
			tx.Rollback()
			return nil, fmt.Errorf("添加商品标签失败: %v", err)
		}
	}

	// 提交事务
	err = tx.Commit().Error
	if err != nil {
		tx.Rollback()
		return nil, fmt.Errorf("提交事务失败: %v", err)
	}

	return nil, nil

}

type EditGoodsStreamer struct {
	GoodsID       int
	InstitutionID int

	StreamerID int
}

// EditGoodsStreamer 执行用户登录逻辑
func (t *EditGoodsStreamer) EditGoodsStreamer() (map[string]interface{}, error) {

	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"goods_id":       t.GoodsID,
	}
	var update = map[string]interface{}{
		"streamer_id": t.StreamerID,
	}
	err := models.EditGoods(maps, update)
	if err != nil {
		return nil, err
	}
	return nil, nil
}

// Login 定义登录请求结构体
type DeleteGoods struct {
	GoodsID       int
	InstitutionID int
}

// DeleteGoods 执行用户登录逻辑
func (t *DeleteGoods) DeleteGoods() (map[string]interface{}, error) {

	var maps = map[string]interface{}{
		"institution_id": t.InstitutionID,
		"goods_id":       t.GoodsID,
	}
	var update = map[string]interface{}{
		"is_delete": 1,
	}
	err := models.EditGoods(maps, update)
	if err != nil {
		return nil, err
	}
	return nil, nil
}
